<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/28
 * Time: 18:12
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Base extends Model
{
    /**
     * 排序取最新的
     */
    public function scopeLatest($query)
    {
        return $query->orderBy('updated_at', 'desc')->orderBy('created_at', 'desc');
    }

    /**
     * 取某时间之后的数据
     */
    public function scopeCreatedAfter($query, $time)
    {
        return $query->where('created_at', '>=', $time);
    }

    /**
     * 取某时间之前的数据
     */
    public function scopeCreatedBefore($query, $time)
    {
        return $query->where('created_at', '<=', $time);
    }

    /**
     * 根据ID修改
     */
    public function scopeUpdateById($query, $id, $data)
    {
        return $query->whereId($id)->update($data);
    }

    /**
     * 搜索
     */
    public function scopeSearch($query, $data, $orwhere = null)
    {
        if ($orwhere) {
            return $query->where($data)->orWhere($orwhere);
        } else {
            return $query->where($data);
        }
    }

    /*
     * 取多条数据ids
     */
    public function scopeIds($query, $ids, $not = false)
    {
        if ($not) {
            return $query->whereIn('id', $ids);
        } else {
            return $query->whereNotIn('id', $ids);
        }
    }

    /**
     * 过滤软关闭了的数据
     */
    public function scopeSwitch($query, $status = 1)
    {
        return $query->where('switch', $status);
    }

    /**
     * 筛选不同周期
     * @param $query
     * @param int $type
     * @return mixed
     */
    public function scopePeriod($query, $period = 1)
    {
        return $query->where('period', $period);
    }

    /**
     * 筛选昨天数据
     * @param $query
     * @param int $type
     * @return mixed
     */
    public function scopeYesterday($query)
    {
        $yesterday = date('Y-m-d', strtotime('yesterday'));
        return $query->whereDate('created_at', $yesterday);
    }

    /**
     * 筛选单天数据
     * @param $query
     * @param int $type
     * @return mixed
     */
    public function scopeDay($query, $day)
    {
        return $query->where('day', '=',$day);
    }

    /**
     * 筛选指定日期数据
     * @param $query
     * @param string $start
     * @param string $end
     * @return mixed
     */
    public function scopeBetweenDay($query, $start, $end)
    {
        return $query->whereBetween('day', [$start, $end]);
    }

    /**
     * 筛选广告位
     * @param $query
     * @param int $positionId
     *
     * @return mixed
     */
    public function scopePosition($query, $positionId)
    {
        if ($positionId) {
            return $query->where('position_id', $positionId);
        } else {
            return $query;
        }
    }

    /**
     * 筛选媒体
     * @param $query
     * @param int $mediaId
     *
     * @return mixed
     */
    public function scopeMedia($query, $mediaId)
    {
        if ($mediaId) {
            return $query->where('media_id', $mediaId);
        } else {
            return $query;
        }
    }

    /**
     * 筛选广告主
     * @param $query
     * @param int $mediaId
     *
     * @return mixed
     */
    public function scopeClient($query, $clientId)
    {
        if ($clientId) {
            return $query->where('client_id', $clientId);
        } else {
            return $query;
        }
    }

    /**
     * 筛选广告主 因前端数据设计不合逻辑，所以这里和上面名字不一样
     * @param $query
     * @param int $mediaId
     *
     * @return mixed
     */
    public function scopeClients($query, $clientId)
    {
        if ($clientId) {
            return $query->where('clients_id', $clientId);
        } else {
            return $query;
        }
    }

    /**
     * 批量插入数据
     * @param  [type] $mobile [description]
     * @return [type]         [中间四个数字用*代替]
     *
     * @author   <[fnn]>
     */
    public function addAll(Array $data)
    {
        $rs = DB::table($this->getTable())->insert($data);
        return $rs;
    }

    /**
     * 修改用户的手机号
     * @param  [type] $mobile [description]
     * @return [type]         [中间四个数字用*代替]
     *
     * @author   <[fnn]>
     */
//    public function getMobileAttribute($mobile)
//    {
//        if (!empty($mobile)) {
//            $resmobile = substr_replace($mobile, '****', 3, 4);
//            return $resmobile;
//        } else {
//            return $mobile;
//        }
//
//    }
}